<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: kane <chengjin005@163.com> 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

/**
 * Class NavController 导航类别管理控制器
 * @package app\admin\controller
 */
class ProjectController extends AdminBaseController
{	

	/**
	 * 项目首页
	 */
	public function index()
	{
		$data = $this->request->param();
		
		//页码
		$now_page = isset($data['page'])?$data['page']:'1';
		//搜索名称
		$keyword = isset($data['keyword'])?$data['keyword']:'';

		//where
		$where = "(cmf_project.project_name like '%$keyword%' or cmf_company.company_name like '%$keyword%' or cmf_customer.customer_name like '%$keyword%' or cmf_customer.customer_mobile like '%$keyword%')";
		//总数
		$sum_result_count = Db::name('project')
								->join('cmf_company','cmf_project.company_id = cmf_company.id','left')
								->join('cmf_customer','cmf_project.customer_id = cmf_customer.id','left')
								->where($where)
								->count();

		$result = Db::name('project')
								->field('cmf_project.*,cmf_company.company_name,cmf_customer.customer_name,cmf_customer.customer_mobile')
								->join('cmf_company','cmf_project.company_id = cmf_company.id','left')
								->join('cmf_customer','cmf_project.customer_id = cmf_customer.id','left')
								->where($where)
								->order("cmf_project.update_time desc")
								->paginate(10,false,['query'=>['keyword'=>$keyword]]);
		$this->assign('result',$result->items());
		$this->assign('page',$result->render());
		$this->assign('now_page',$now_page);
		$this->assign('sum_result_count',$sum_result_count);
		$this->assign('keyword',$keyword);
		return $this->fetch();
	}

	/**
	 * 项目查找页面
	 */
	public function find()
	{
		return $this->fetch();
	}


	/**
	 * 项目查询页面结果
	 */
	public function find_result()
	{
		$data = $this->request->param();
		$project_name = $data['sid'];
		$result = Db::name('project')->where("project_name like '%$project_name%'")->select()->toArray();
		$str = '';
		if(!$result)
		{	
			$result[0]['project_name'] = '';
			$result[0]['id'] = 0;
		}
		foreach ($result as $k => $v) {
        	$datas[] = "{'keywords':'" . $v['project_name'] . "','project_id':'" . $v['id'] . "'}";
        }
        $str = '[' . implode(',', $datas) . ']';
        echo $str;
        die;
	}

	//项目搜索 ajax
	public function search()
	{
		$data = $this->request->param();
		$company_id = $data['company_id'];
		$result = Db::name('project')->where(['company_id'=>$company_id])->select();
		ajaxReturn(['data'=>$result]);
	}



	/**
	 * 新建项目列表
	 */
	public function add()
	{
		return $this->fetch();
	}

	/**
	 * 新建项目
	 */
	public function addPost()
	{
		$post = $this->request->param();
		$data = $post['post'];
		$data['project_desc'] = isset($data['project_desc'])?html_entity_decode(htmlspecialchars_decode($data['project_desc'])):'';
		$data['add_time']    = time();
		$data['update_time'] = $data['add_time'];
		Db::name('project')->insert($data);

		$project_id = Db::name('project')->getLastInsID();
		Db::name('project_customer')->insert(['project_id'=>$project_id,'customer_id'=>$data['customer_id']]);
		$this->success("添加成功！", url("project/index"));
	}

	/**
	 * 项目编辑页面
	 */
	public function edit()
	{
		$data = $this->request->param();
		$id   = $data['id'];
		$result = Db::name('project')
						->field('cmf_project.*,cmf_company.company_name,cmf_customer.customer_name')
						->join('cmf_company','cmf_project.company_id = cmf_company.id','left')
						->join('cmf_customer','cmf_project.customer_id = cmf_customer.id','left')
						->where(['cmf_project.id'=>$id])
						->find();
		$this->assign('result',$result);
		return $this->fetch();
	}


	/**
	 * 项目编辑
	 */
	public function editPost()
	{
		$post = $this->request->param();
		$id   = $post['id'];
		$data = $post['post'];
		$data['project_desc'] = isset($data['project_desc'])?html_entity_decode(htmlspecialchars_decode($data['project_desc'])):'';
		$data['update_time']  = time();
		$data['start_time']   = strtotime($data['start_time']);
		Db::name('project')->where(['id'=>$id])->update($data);
		$this->success('修改成功',url("project/index"));
	}


	/**
	 * 项目详情
	 */
	public function detail()
	{
		$id = $this->request->param('id');
		$result = Db::name('project')
							->field('cmf_project.*,cmf_company.company_name')
							->join('cmf_company','cmf_company.id = cmf_project.company_id','left')
							->where(['cmf_project.id'=>$id])
							->find();

		//项目负责人
		$project_customer_fuze = Db::name('customer')->where(['id'=>$result['customer_id']])->select()->toArray();

		//项目其他联系人
		$other_project_customer= Db::name('project_customer')->where(['project_id'=>$id])->select()->toArray();
		$other_project_customer_ids = [];
		foreach($other_project_customer as $k=>$v)
		{
			if($v['customer_id'] != $result['customer_id'])
			{
				$other_project_customer_ids[] = $v['customer_id'];
			}	
		}
		$other_project_customer_ids = implode(',',$other_project_customer_ids);
		$other_customer = Db::name('customer')->where(['id'=>['in',$other_project_customer_ids]])->select()->toArray();

		$status = $this->request->param('status');
		$status = isset($status)?$status:1; //1显示 2 隐藏
		$this->assign('status',$status);

		$this->assign('other_customer',$other_customer);
		$this->assign('project_customer_fuze',$project_customer_fuze);
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 变更负责人
	 */
	public function changeCustomer()
	{
		$data = $this->request->param();
		Db::name('project')->where(['id'=>$data['project_id']])->update(['customer_id'=>$data['customer_id']]);
		ajaxReturn(['status'=>1]);
	}

	

	/**
	 * 项目测试地址编辑页面
	 */
	public function testBlocked()
	{	
		//id
		$id = $this->request->param('id');
		$result = Db::name('project')->where(['id'=>$id])->find();
		$result['project_test_blocked'] = json_decode($result['project_test_blocked'],true);
		if(!$result['project_test_blocked'])
		{
			$result['project_test_blocked']['admin_address'] = '';
			$result['project_test_blocked']['home_address'] = '';
		}
		
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 项目测试地址编辑
	 */
	public function testBlockedPost()
	{
		$id = $this->request->param('id');
		$data = $this->request->param();
		$result['project_test_blocked'] = json_encode($data['post']);
		Db::name('project')->where(['id'=>$id])->update($result);
		$this->success('编辑成功');
	}

	/**
	 *项目节点图页面
	 */
	public function nodeGraph()
	{
		$id = $this->request->param('id');
		$result = Db::name('project')->where(['id'=>$id])->find();
		$result['project_node_graph'] = json_decode($result['project_node_graph'],true);
		if(!$result['project_node_graph'])
		{
			for($i = 1; $i<=15 ; $i++)
			{
				$result['project_node_graph'][$i] = '';
			}
			
		}
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 项目节点图编辑
	 */
	public function nodeGraphPost()
	{
		$id = $this->request->param('id');		
		$data = $this->request->param();
		$result['project_node_graph'] = json_encode($data['post']);
		$result['project_status']     = isset($data['project_status'])?$data['project_status']:1;
		Db::name('project')->where(['id'=>$id])->update($result);
		$this->success('编辑成功');

	}

	/**
	 * 项目过程列表 (开发需求确认)
	 */
	public function process()
	{
		$id = $this->request->param('id');

		//开发需求
		$result = Db::name('development_demand')->where(['project_id'=>$id])->order("cmf_development_demand.update_time desc")->select()->toArray();
		$this->assign('result',$result);
		$this->assign('id',$id);
		return $this->fetch();
	}

	/**
	 * 项目需求添加页面
	 */
	public function develDemand()
	{
		$id = $this->request->param('id');
		$this->assign('id',$id);

		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);

		return $this->fetch();
	}

	/**
	 * 项目需求添加
	 */
	public function develDemandPost()
	{
		$data = $this->request->param();
		$result['project_id'] = $data['id'];
		$result['content']    = $data['content'];
		$result['add_time']   = time();
		$result['update_time']= $result['add_time'];
		Db::name('development_demand')->insert($result);

		Db::name('check')->insert($result);
		$this->success('添加成功',url('project/develDemand?id='.$data['id'].'&status=2'));
	}
	/**
	 * 项目需求编辑页面
	 */
	public function develDemandEdit()
	{
		$data = $this->request->param();
		//需求id
		$id = $data['id'];
		$result = Db::name('development_demand')->where(['id'=>$id])->find();

		//项目id
		$project_id = $data['project_id'];
		$this->assign('result',$result);
		$this->assign('project_id',$project_id);

		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);

		return $this->fetch();
	}
	
	/**
	 * 项目需求编辑
	 */
	public function develDemandEditPost()
	{
		$data = $this->request->param();
		//需求id
		$id = $data['id'];
		//项目id
		$project_id = $data['project_id'];
		$result['content'] = $data['content'];
		$result['update_time'] = time();
		Db::name('development_demand')->where(['id'=>$id])->update($result);
		$this->success('修改成功',url('project/develDemand?id='.$project_id.'&status=2'));

	}

	/**
	 * 客户变更意见列表
	 */
	public function customerChangeOpinion()
	{	
		$id = $this->request->param('id');
		$result = Db::name('change_opinion')
								->field('cmf_change_opinion.*,cmf_customer.customer_name')
								->join('cmf_customer','cmf_customer.id = cmf_change_opinion.customer_id','left')
								->where(['cmf_change_opinion.project_id'=>$id])
								->order("cmf_change_opinion.update_time desc")
								->select()
								->toArray();
		$this->assign('result',$result);
		$this->assign('id',$id);
		return $this->fetch();
	}

	/**
	 * 添加解决方案页面
	 */
	public function solutionAdd()
	{
		$data = $this->request->param();
		//意见id
		$id = $data['id'];
		//项目id
		$project_id = $data['project_id'];
		//项目负责人姓名
		$project_data  = Db::name('project')->field('customer_id')->where(['id'=>$project_id])->find();
		$customer_data = Db::name('customer')->field('id,customer_name')->where(['id'=>$project_data['customer_id']])->find();
		$this->assign('result',$customer_data);
		$this->assign('id',$id);
		$this->assign('project_id',$project_id);

		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);

		return $this->fetch();
	}

	/**
	 * 添加解决方案
	 */
	public function solutionAddPost()
	{
		$data = $this->request->param();
		$data['add_time']    = time();
		$data['update_time'] = $data['add_time'];
		$data['apply_time']  = strtotime($data['apply_time']);
		Db::name('solution')->insert($data);
		$this->success('添加成功',url('project/solutionAdd?project_id='.$data['project_id'].'&status=2&id='.$data['change_opinion_id']));
	}


	/**
	 *编辑解决方案页面 
	 */
	public function solutionEdit()
	{
		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);

		$data = $this->request->param();
		//意见id
		$id = $data['id'];
		//项目id
		$project_id = $data['project_id'];

		$result = Db::name('solution')
								->field('cmf_solution.*,cmf_customer.customer_name')
								->join('cmf_customer','cmf_customer.id = cmf_solution.customer_id','left')
								->where(['cmf_solution.id'=>$id])
								->find();

		$this->assign('result',$result);
		$this->assign('id',$id);
		$this->assign('project_id',$project_id);
		return $this->fetch();
	}

	/**
	 * 解决方案修改
	 */
	public function solutionEditPost()
	{
		$data = $this->request->param();
		//意见id
		$id = $data['id'];
		//项目id
		$project_id = $data['project_id'];
		$data['apply_time'] = strtotime($data['apply_time']);
		$data['update_time']= time();
		unset($data['id']);unset($data['project_id']);
		Db::name('solution')->where(['id'=>$id])->update($data);
		$this->success('修改成功',Url('project/solutionEdit?id='.$id.'&project_id='.$project_id.'&status=2'));
	}
	
	/**
	 *关闭意见页面 
	 */
	public function closeOpinion()
	{
		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);

		$data = $this->request->param();
		//意见id
		$id = $data['id'];
		//项目id
		$project_id = $data['project_id'];

		$result = Db::name('change_opinion')->where(['id'=>$id])->find();
		$this->assign('result',$result);
		$this->assign('id',$id);
		$this->assign('project_id',$project_id);
		return $this->fetch();
	}

	/**
	 * 关闭意见
	 */
	public function closeOpinionPost()
	{
		$data = $this->request->param();
		//意见id
		$id = $data['id'];
		//项目id
		$project_id = $data['project_id'];
		//关闭原因
		$result['close_desc'] = isset($data['close_desc'])?html_entity_decode(htmlspecialchars_decode($data['close_desc'])):'';
		$result['status']     = 2;
		$result['update_time']= time();
		Db::name('change_opinion')->where(['id'=>$id])->update($result);
		$this->success('关闭成功',Url('project/closeOpinion?id='.$id.'&project_id='.$project_id.'&status=2'));
	}
	
	/**
	 * 变更列表
	 */
	public function changeOpinion()
	{
		$id = $this->request->param('id');
		$this->assign('id',$id);

		$result = Db::name('solution')
						->field('cmf_solution.*,cmf_customer.customer_name')
						->join('cmf_customer','cmf_customer.id = cmf_solution.customer_id','left')
						->where(['project_id'=>$id])
						->order('cmf_solution.update_time desc')
						->select()
						->toArray();
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 查看修改意见
	 */
	public function selectImgContent()
	{
		$id = $this->request->param('id');
		$result = Db::name('home_img_content')->where(['home_img_id'=>$id])->select()->toArray();
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * PC效果图确认
	 */
	public function effectImgConfirm()
	{
		$id = $this->request->param('id');
		$this->assign('id',$id);

		$result = Db::name('home_img')->where(['project_id'=>$id])->order("cmf_home_img.add_time desc")->select()->toArray();
		$this->assign('result',$result);

		$other_result = Db::name('home_other_img')->where(['project_id'=>$id])->order("cmf_home_other_img.add_time desc")->select()->toArray();
		$this->assign('other_result',$other_result);
		return $this->fetch();
	}

	/**
	 * 首页效果图添加页面
	 */
	public function homeImgAdd()
	{
		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);

		$id = $this->request->param('id');
		$this->assign('id',$id);
		return $this->fetch();
	}

	/**
	 * 首页效果图添加
	 */
	public function homeImgAddPost()
	{
		$id = $this->request->param('id');

		$file = request()->file('file');
		if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'effect/upload_xiaoguotu');
	        if($info){    
	            $file_name =  $info->getSaveName();   
	            $result['project_id'] = $id;
	            $result['home_img']   = $file_name;
	            $result['add_time']   = time();
	            $result['update_time']= time();
	            

	            //生成静态页面
	            $str = "<meta charset='utf-8'>
						<img src='../upload_xiaoguotu/".$file_name."' ondragstart='return false'  alt='' style='width: 100%;' >
						<script>  
							function Click(){ 
								window.event.returnValue=false; 
							} 
							document.oncontextmenu=Click; 
						</script>";
				$file_name = explode('.', $info->getFilename())[0].'.html';
				$result['home_img_address'] = $file_name;
							
				Db::name('home_img')->insert($result);
				
				$save_file_path = CMF_ROOT.'/public/effect/file/'.$file_name;
				file_put_contents($save_file_path, $str);


	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
    	}
    	exit();
	}


	/**
	 * 其他效果图添加
	 */
	public function homeImgOtherAdd()
	{
		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);
		
		$id = $this->request->param('id');
		$this->assign('id',$id);
		return $this->fetch();
	}

	public function homeImgOtherAddPost()
	{
		$id = $this->request->param('id');

		$file = request()->file('file');
		if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'effect/upload_xiaoguotu');
	        if($info){    
	            $file_name =  $info->getSaveName();   
	            $result['project_id'] = $id;
	            $result['img']   = $file_name;
	            $result['add_time']   = time();
	            $result['update_time']= time();
	           				
				Db::name('home_other_img')->insert($result);

	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
    	}
    	exit();
	}


	/**
	 * mobile效果图确认
	 */
	public function mobileImg()
	{
		$id = $this->request->param('id');
		$this->assign('id',$id);

		$result = Db::name('mobile_home_img')->where(['project_id'=>$id])->order("cmf_mobile_home_img.add_time desc")->select()->toArray();
		$this->assign('result',$result);

		$other_result = Db::name('mobile_home_other_img')->where(['project_id'=>$id])->order("cmf_mobile_home_other_img.add_time desc")->select()->toArray();
		$this->assign('other_result',$other_result);
		return $this->fetch();
	}


	/**
	 * mobile首页效果图添加页面 
	 */
	public function mobileHomeImgAdd()
	{
		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);

		$id = $this->request->param('id');
		$this->assign('id',$id);
		return $this->fetch();
	}

	/**
	 * mobile首页效果图添加
	 */
	public function mobileHomeImgAddPost()
	{
		$id = $this->request->param('id');

		$file = request()->file('file');
		if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'effect/upload_xiaoguotu_mobile');
	        if($info){    
	            $file_name =  $info->getSaveName();   
	            $result['project_id'] = $id;
	            $result['home_img']   = $file_name;
	            $result['add_time']   = time();
	            $result['update_time']= time();
	            

	            //生成静态页面
	            $str = "<meta charset='utf-8'>
						<img src='../upload_xiaoguotu_mobile/".$file_name."' ondragstart='return false'  alt='' style='width: 100%;' >
						<script>  
							function Click(){ 
								window.event.returnValue=false; 
							} 
							document.oncontextmenu=Click; 
						</script>";
				$file_name = explode('.', $info->getFilename())[0].'.html';
				$result['home_img_address'] = $file_name;
							
				Db::name('mobile_home_img')->insert($result);
				
				$save_file_path = CMF_ROOT.'/public/effect/file_mobile/'.$file_name;
				file_put_contents($save_file_path, $str);


	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
    	}
    	exit();
	}


	/**
	 * mobile其他效果图添加
	 */
	public function mobileHomeImgOtherAdd()
	{
		$status = $this->request->param('status');
		$status = isset($status)?$status:1;
		$this->assign('status',$status);
		
		$id = $this->request->param('id');
		$this->assign('id',$id);
		return $this->fetch();
	}

	public function mobileHomeImgOtherAddPost()
	{

		$id = $this->request->param('id');

		$file = request()->file('file');
		if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'effect/upload_xiaoguotu_mobile');
	        if($info){    
	            $file_name =  $info->getSaveName();   
	            $result['project_id'] = $id;
	            $result['img']   = $file_name;
	            $result['add_time']   = time();
	            $result['update_time']= time();
				Db::name('mobile_home_other_img')->insert($result);

	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
    	}
    	exit();
	}

	/**
	 * mobile查看修改意见
	 */
	public function mobileSelectImgContent()
	{
		$id = $this->request->param('id');
		$result = Db::name('mobile_home_img_content')->where(['home_img_id'=>$id])->select()->toArray();
		$this->assign('result',$result);
		return $this->fetch();
	}


	/**
	 * 验收列表
	 */
	public function check()
	{
		$id = $this->request->param('id');
		$this->assign('id',$id);
		$result = Db::name('check')->where(['project_id'=>$id])->order("cmf_check.add_time desc")->select()->toArray();
		$this->assign('result',$result);

		$result1 = Db::name('checkout')->where(['project_id'=>$id])->order("add_time desc")->find();
		$this->assign('result1',$result1);
		return $this->fetch();
	}

	/**
	 * 意见状态更改
	 */
	public function changeCheckStatus()
	{
		$data = $this->request->param();
		Db::name('check')->where(['id'=>$data['id']])->update(['status1'=>$data['status']]);
	}
	/**
	 * 交付文档
	 */
	public function deliver()
	{
		$id = $this->request->param('id');
		$this->assign('id',$id);

		$result = Db::name('deliver_file')->where(['project_id'=>$id])->order("cmf_deliver_file.add_time desc")->select()->toArray();
		$this->assign('result',$result);
		return $this->fetch();
	}

	/**
	 * 上传附件文件
	 * @return [type] [description]
	 */
	public function uploadFile()
	{
		$id = $this->request->param('id');

		$file = request()->file('file');
		if($file){
			$file_before_name = iconv("UTF-8","gb2312", $_FILES['file']['name']);;
			$file->saveRule = false;
			$date = date('Ymd').'/';
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'effect/word',$date.$file_before_name);
	        if($info){    
	            $result['project_id'] = $id;
	            $result['file_name']  = $date.iconv("gb2312","UTF-8", $file_before_name);
	            $result['name']  = iconv("gb2312","UTF-8", $file_before_name);
	            $result['add_time']   = time();
	            $result['update_time']= time();
	           				
				Db::name('deliver_file')->insert($result);

	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
    	}
    	exit();
	}

	/**
	 * 改变文件状态
	 */
	public function changeFileStatus()
	{
		$data = $this->request->param();
		Db::name('deliver_file')->where(['id'=>$data['id']])->update(['status'=>$data['status']]);
	}
}